package com.zb.tools;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

public class HealthReport {

    public static class LoginData {
        public static class UserData {
            public String id;
            public String username;
        }

        public static class Data {
            public UserData user;
            public String token;
        }

        public int code;
        public String message;
        public Data data;
    }

    public static class ReportData {
        public static class Data {
            public String id;
            public String userId;
            public String province;
            public String provinceCode;
            public String city;
            public String cityCode;
            public String notReturnReason;                 //未返回原因，null,
            public String healthState;                     //null
            public String healthStateDescription;          //身体健康
            public String isWorkPosition;                  //在单位所在地 1 or 0
            public String workType;                        //null
            public String workTypeDescription;
            public String isPersonnel;
            public String isPersonnelDescription;
            public String isIll;                           //确诊
            public String illTime;
            public String curePosition;
            public String illDescription;
            public String isSuspected;                     //疑似
            public String suspectedPosition;
            public String suspectedTime;
            public String suspectedEndTime;
            public String suspectedDescription;
            public String isConsensus;
            public String consensusBeginTime;
            public String consensusEndTime;
            public String consensusPosition;
            public String isHome;                          //居家
            public String homeReason;
            public String homeBeginTime;
            public String homeEndTime;
            public String homeHealth;
            public String isTouch;                         //密切接触
            public String touchReason;
            public String todayIsTouchFever;
            public String todayIsTouchFeverDescription;
            public String outherData;
            // String[] files;
            // String[] sysFiles;
        }

        public int code;
        public String message;
        public Data data;
    }


    public static void main(String[] args) throws IOException {


        String report=StringKit.readFile("report.json","UTF-8");

        IniKit.IniFile ini=IniKit.use("config.ini","utf-8");

        String url_login=ini.get("net","url_login");
        String url_report=ini.get("net","url_report");
        String url_list=ini.get("net","url_list");
        String username=ini.get("user","username");
        String password=ini.get("user","password");

        boolean debug=ini.getBoolean("net","debug",false);

        OkHttpKit kit=OkHttpKit.create();

        //登录
        String url=String.format(url_login, username,password);
        String json=kit.postJson(url,null,null);
        if(debug)
        {
            System.out.println("[info]:url="+url);
            System.out.println("[recv]:");
            System.out.println(json);
        }
        LoginData loginData= JSON.parseObject(json,LoginData.class);
        System.out.format("[info]:%s(%d)\n",loginData.message,loginData.code);
        if(loginData.code!=200)
        {
            System.exit(-1);
        }
        System.out.format("[info]:userid=%s\n",loginData.data.user.id);
        System.out.format("[info]:token=%s\n",loginData.data.token);
        //报送


        report=report.replace("${uuid}",UUID.randomUUID().toString().replace("-",""))
                .replace("${userid}",loginData.data.user.id)
                .replace("${date}",new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        if(debug)
        {
            System.out.println("[info]:url="+url_report);
            System.out.println("[send]:");
            System.out.println(report);
        }
        HashMap<String,String> headers=new HashMap<String,String>();
        headers.put("Authorization",loginData.data.token);
        json=kit.postJson(url_report,report,headers);
        if(debug)
        {
            System.out.println("[recv]:");
            System.out.println(json);
        }
        ReportData reportData=JSON.parseObject(json,ReportData.class);
        System.out.format("[info]:%s(%d)\n",reportData.message,reportData.code);
        if(reportData.code!=200)
        {
            System.exit(-1);
        }
        System.out.println("[info]:发送成功！");
    }
}
